<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/11/7 13:47
 * Intro:
 */

namespace addons\podunk\controller;


use addons\podunk\library\traits\GoldCache;
use addons\podunk\library\Curl;
use addons\podunk\library\Util;
use think\Exception;
use think\Log;
use think\Request;

class TiktokStore extends Base
{
    use GoldCache;

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        return parent::_initialize();
    }

    public function addBrushTask()
    {
        try{
            $params = $this->request->post();

            if($params['device_select_mode'] == 'deviceGroupMode'){
                if($params['device_group_ids']){
                    $deviceGroupIds = implode(',', $params['device_group_ids']);
                    $deviceIds = \addons\podunk\model\Device::where(['group_id'=>array('in',$deviceGroupIds),'user_id'=>$this->auth->id])->column('id,identity');
                }else{
                    $this->error('空的设备分组');
                }
            }else{
                $deviceIds = \addons\podunk\model\Device::where(['id'=>array('in',$params['device_ids']), 'user_id'=>$this->auth->id])->column('id,identity');
            }

            switch($params['open_live_mode']){
                case 'tiktok_id':
                    $openLiveText =trim($params['open_live_text']);
                    break;
                case 'works_share':
                    $openLiveText = $this->sharetextToVid($params['open_live_text']);
                    break;
                case 'personal_share':
                    $openLiveText = $this->sharetextToVid($params['open_live_text']);
                    break;
                default:
                    break;
            }

            if($params['start_mode'] == 'now'){
                $time = time();
            }else{
                $time = time() + ($params['delay'] * 60);
            }
            foreach ($deviceIds as $key=>$value){
                $data = [];
                $data['task_name'] = $params['task_name'];
                $data['app'] = 'tiktok_store';
                $data['task'] ='brush';
                $data['open_live_mode'] = $params['open_live_mode'];
                $data['open_live_text'] = $openLiveText;
                $data['product_name'] = $params['product_name'];
                $data['start_time'] = $time;
                $data['uuid'] = Util::uuid();
                $data = json_encode($data);
                $this->pushTaskToKey($value.'_'.$this->auth->id.'_'.$key, $data);
            }

        }catch (Exception $e){
            Log::error(Request::instance()->controller().'---'.$e->getMessage());
            $this->error($e->getMessage());
        }

        $this->success('添加成功');
    }

    public function addOrderReturnTask()
    {
        try{
            $params = $this->request->post();

            $deviceId = $params['device_id'];
            $identity = $params['identity'];
            $time = time();
            $data = [];
            $data['task_name'] = $params['task_name'];
            $data['app'] = 'tiktok_store';
            $data['task'] ='order_return';
            $data['order_id'] = $params['order_id'];
            $data['start_time'] = $time;
            $data['comment'] = $params['comment'];
            $data['image'] = $params['image'];
            $data['uuid'] = Util::uuid();
            $data = json_encode($data);
            //Log::debug(Request::instance()->controller().'---'.$data);
            $this->pushTaskToKey($identity.'_'.$this->auth->id.'_'.$deviceId, $data);
        }catch (Exception $e){
            Log::error(Request::instance()->controller().'---'.$e->getMessage());
            $this->error($e->getMessage());
        }

        $this->success('添加成功');
    }

    public static function sharetextToVid($text){
        if(preg_match('/[a-zA-z]+:\/\/[^\s]*/',$text,$short_url)){
            $url = $short_url[0];
        }else{
            return false;
        }

        $source =  Curl::getall($url);

        if(preg_match('/(?<=\/)[1-9][0-9]*/',$source,$vid)){
            return $vid[0];
        }else{
            return false;
        }
        return false;
    }

    public function livetextToUrl($text){
        preg_match_all("/https:[\/]{2}[a-z]+[.]{1}[a-z\d\-]+[.]{1}[a-z\d]*[\/]*[A-Za-z\d]*[\/]*[A-Za-z\d]*/",$text,$arr);
        return $arr[0][0];
    }

}